package cn.kuaipai.mcard.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.kuaipai.mcard.service.AuthorizeManager;
import cn.kuaipai.weibo.api.tencent.TencentUserAPI;
import cn.kuaipai.weibo.constants.WeiboConstants;
import cn.kuaipai.weibo.http.OAuthProvider;
import cn.kuaipai.weibo.model.OAuth;
import cn.kuaipai.weibo.utils.APIURL;

/**
 * 用户鉴权controller
 * @author zy
 */
@Controller
public class AuthorizeController {
	protected final Logger logger = Logger.getLogger(AuthorizeController.class);
	
	@Resource
	private AuthorizeManager authorizeManager ;
	
	//请求requestToken,并且跳转至腾讯授权页面
	@RequestMapping(value = "/authorize/requestToken.do", method = RequestMethod.GET)
	public void requestToken(HttpServletRequest request,HttpServletResponse response, HttpSession session, ModelMap map) {
		try {
		OAuthProvider oauthProvider = new OAuthProvider();
		OAuth oauth = oauthProvider.requestToken(new OAuth(),APIURL.getRequestTokenURL());
		session.setAttribute("oauth",oauth);
		System.out.println(oauth.getOauthToken() + " " + oauth.getOauthTokenSecret());
		response.sendRedirect(oauthProvider.constructAuthorizeURL(APIURL.getAuthorizeURL(),oauth.getOauthToken()));
		} catch (IOException e) {
			logger.error("[authorize] requestToken error !", e);
		}
	}
	
	//通过腾讯平台用户名密码输入,返回进行授权处理
	// TODO 可以进行用户名、密码的绑定
	@RequestMapping(value = "/authorize/process.do", method = RequestMethod.GET)
	public void authorizeProcess(HttpServletRequest request,HttpServletResponse response, HttpSession session, ModelMap map) {
		try {
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			String oauthVerifier = request.getParameter("oauth_verifier");
			String openid =  request.getParameter("openid");
			String openkey = request.getParameter("openkey");
			System.out.println(openid + " " + openkey);
			if(StringUtils.isBlank(oauthVerifier)){
				out.print("get verifier error!");
			}else{
				OAuthProvider oauthProvider = new OAuthProvider();
				OAuth oauth = (OAuth)session.getAttribute("oauth");
				oauth.setOauthVerifier(oauthVerifier);
				oauth.setOpenid(openid);
				oauth.setOpenkey(openkey);
				oauth = oauthProvider.accessToken(oauth,APIURL.getAccessTokenURL());
				System.out.println(oauth.getOauthToken() + " " + oauth.getOauthTokenSecret());
				
				out.print("<br/>*******************************[oauth1.0]**************************************<br/>");
				TencentUserAPI tencent = new TencentUserAPI();
				out.print(tencent.getUserInfo(WeiboConstants.FORMAT_RESPONSE_JSON,oauth));
				
				out.print("<br/>*******************************[openid&openkey]**************************************<br/>");
				out.print(tencent.getUserInfo(WeiboConstants.FORMAT_RESPONSE_JSON,openid,openkey));
				
				
				if(authorizeManager.getAuthorizeByOpenid(openid) == null){
					System.out.println("dd");
				}
			}
		} catch (IOException e) {
			logger.error("[authorize] requestToken error !", e);
		}
	}
}
